<?php
class Main extends Kcontroller {
	function login() {
		$data = array();
		$data["error"] = Ksession::getFlash("error");
		$this->load->template('templates/login','main/login', $this->k_application_title, $data);
	}
	
	function submitlogin() {
		$this->load->model("usermodel");
		$user = $_POST["user_login"];
		$pwd = $_POST["user_pwd"];
		var_dump($_POST);
		$this->load->model("usermodel");
		$user = $this->usermodel->submitlogin($user, $pwd);
		if ($user == null) :
			$error = Ksession::setFlash("error", "User name or Password is not correct");
			redirect(site_url("main/login"));
			return;
		else:
			Ksession::setUser($user);
			redirect(site_url("main/companyselection"));
		endif;
		
	}
	
	function companyselection() {
		$user = Ksession::getUser();
		if (isset($user["selected_company"])) :
			unset($user["selected_company"]);
		endif;
		Ksession::setUser($user);
		
		Ksession::removeCriteria("comsel");
		$data = array();
		$this->load->model("companymodel");
		$data["comapny_list"] = $this->companymodel->getAllCompany();
		$data["error"] = Ksession::getFlash("error");
		$this->load->template('templates/companyselection','main/com_selection', $this->k_application_title, $data);
	}
	
	function home() {
		$data = array();
		$this->load->template('templates/general','main/home', $this->k_application_title, $data);
		
	}
	
	function logout() {
		Ksession::destroy();
		redirect(site_url());
	}
	
	function submitselectioncompany() {
		if (!isset($_POST["com_id"]) || $_POST["com_id"] == "") :
			redirect(site_url("main/companyselection"));
			return;
		endif;
		$this->load->model("companymodel");
		$company = $this->companymodel->getCompanyById($_POST["com_id"]);
		$user = Ksession::getUser();
		$user["selected_company"] = $company;
		Ksession::setUser($user);
		
		redirect(site_url("main/home"));
	}
}